package com.jtjrenren.android.taxi.driver.manger.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;
import com.jtjrenren.android.taxi.driver.base.BaseApplication;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.library.utils.LogUtil;
import com.library.utils.StringUtils;

public class PushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_MSG_DATA:
                // 获取透传数据
                byte[] payload = bundle.getByteArray("payload");
                String taskid = bundle.getString("taskid");
                String messageid = bundle.getString("messageid");

                // smartPush第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行
                boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
                System.out.println("第三方回执接口调用" + (result ? "成功" : "失败"));

                if (payload != null) {
                    String data = new String(payload);
                    LogUtil.log("push", "接收到透传消息: " + data);
                    if (!StringUtils.isEmpty(data))
                        PushCmdEngine.handReceiveMsg(data);
                }
                break;
            case PushConsts.GET_CLIENTID:
                // 获取ClientID(CID)
                String cid = bundle.getString("clientid");
                AppManger.getInstance().setGtCID(cid);
                Log.d("push", "clientid : " + cid);
                break;
            case PushConsts.GET_SDKONLINESTATE:
                boolean online = bundle.getBoolean("onlineState");
                Log.d("push", "online = " + online);
                if (!online) {
                    LogUtil.log("push","离线进行初始化");
                    PushManager.getInstance().initialize(BaseApplication.getInstance().getApplicationContext());
                }
                break;
            default:
                break;
        }
    }
}
